﻿using NPOI.SS.Formula.Functions;

using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.StationApp.Dtos;
using PLCS.Application.Contracts.TaskApp.Dtos;
using PLCS.Client.IService.LogManager;
using PLCS.Client.IService.OrderTask;
using PLCS.Domain.Shared.Enums;
using PLCS.Domain.Shared.Tasks;
using PLCS.Shared.Common.Constant;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace PLCS.App.ViewModels.Dialogs;

public class RowGoodsDetailViewModel : HostDialogViewModel
{
    private string _startPos, _endPos, _hintMessage, _trayNo;
    private int _columnCount, _roadWay, _row;
    private ObservableCollection<RowGoodsInfo> _rowStations;
    private readonly ITaskService _taskService;
    private int _level;

    public int ColumnCount
    {
        get { return _columnCount; }
        set { SetProperty(ref _columnCount, value); }
    }

    public int RoadWay
    {
        get { return _roadWay; }
        set { SetProperty(ref _roadWay, value); }
    }

    public int Row
    {
        get { return _row; }
        set { SetProperty(ref _row, value); }
    }

    public string StartPos
    {
        get { return _startPos; }
        set { SetProperty(ref _startPos, value); }
    }

    public string EndPos
    {
        get { return _endPos; }
        set { SetProperty(ref _endPos, value); }
    }

    public string HintMessage
    {
        get { return _hintMessage; }
        set { SetProperty(ref _hintMessage, value); }
    }

    public string TrayNo
    {
        get { return _trayNo; }
        set { SetProperty(ref _trayNo, value); }
    }

    public int Level
    {
        get { return _level; }
        set { SetProperty(ref _level, value); }
    }

    public DelegateCommand SendTaskCommand { get; }

    public ObservableCollection<RowGoodsInfo> RowStations
    {
        get { return _rowStations; }
        set { SetProperty(ref _rowStations, value); }
    }

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        if (parameters == null)
            return;
        var platepos = parameters.GetValue<string>("Platepos");
        var res = parameters.GetValue<IEnumerable<StationDto>>("Models").Select(x => new RowGoodsInfo
        {
            Roadway = int.Parse(x.StaNum.Split('-')[0]),
            Row = int.Parse(x.StaNum.Split('-')[1]),
            Column = int.Parse(x.StaNum.Split('-')[2]),
            Layer = int.Parse(x.StaNum.Split('-')[3]),
            Name = $"{x.StaNum.Split('-')[2]}-{x.StaNum.Split('-')[3]}",
            Status = x.IsUse == YesNo.是 ? (int)x.HaveStatus : 2,
            TrayNo = x.Trays?.FirstOrDefault()?.TrayNo
        });

        RowStations = platepos == "左"
            ? res.OrderByDescending(x => x.Layer).ThenBy(x => x.Column).ToObservable()
            : res.OrderByDescending(x => x.Layer).ThenByDescending(x => x.Column).ToObservable();

        if (!RowStations.Any())
            return;
        RoadWay = RowStations.First().Roadway;
        Row = RowStations.First().Row;
        ColumnCount = int.Parse(parameters.GetValue<string>("ColumnCount"));
    }

    public RowGoodsDetailViewModel(ITaskService taskService)
    {
        View = "货位弹窗";
        SendTaskCommand = new DelegateCommand(SendTask);
        _taskService = taskService;
    }

    private async void SendTask()
    {
        try
        {
            SavebuttonIsEnabled = false;
            if (string.IsNullOrEmpty(EndPos))
            {
                HintMessage = "目标位不可为空";
                return;
            }
            if (string.IsNullOrEmpty(TrayNo) && string.IsNullOrEmpty(StartPos))
            {
                HintMessage = "托盘号和起始位必须要有一个";
                return;
            }
            var task = new TaskEditDto()
            {
                EndPos = EndPos,
                TrayNo = TrayNo,
                Level = Level,
                StartPos = StartPos,
                TaskType = TaskType.出库
            };
            await _taskService.CreateAsync(task);
            HintMessage = "下发成功";
            Log("下发任务", task);
        }
        catch (Exception ex)
        {
            HintMessage = ex.Message;
        }
        finally
        {
            SavebuttonIsEnabled = true;
        }
    }

    public class RowGoodsInfo
    {
        public int Roadway { get; set; }
        public int Row { get; set; }
        public int Column { get; set; }
        public int Layer { get; set; }
        public string Name { get; set; }
        public int Status { get; set; }
        public string TrayNo { get; set; }
    }
}